﻿using B.MES.Customer.Domain;
using B.MES.Customer.Domain.DTO;
using B.MES.Customer.Infrastructure.IRepository;
using SqlSugar;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace B.MES.Customer.Infrastructure.Repository
{
    /// <summary>
    /// 物料仓储实现层
    /// </summary>
    public class MaterialRepository : BaseRepository<Material>, IMaterialRepository
    {
        private readonly ISqlSugarClient _db;
        private readonly CSRedis.CSRedisClient _cSRedis;
        public MaterialRepository(ISqlSugarClient db) : base(db)
        {
            _db = db;
            _cSRedis = new CSRedis.CSRedisClient("10.31.59.13");
        }
        /// <summary>
        /// 带分页的物料查询
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="matCode">物料编号</param>
        /// <param name="matName">物料名称</param>
        /// <param name="matModel">规格型号</param>
        /// <param name="matBrand">品牌</param>
        /// <returns>分页查询结果</returns>
        public PageApiResut<MaterialDTO> GetMaterials(int pageIndex, int pageSize, string? matCode, string? matName, string? matModel, string? matBrand)
        {
            PageApiResut<MaterialDTO> pageApiResult = new PageApiResut<MaterialDTO>();

            var query = _db.Queryable<Material>();
            var mlist = _db.Queryable<Matattribute>();
            var mtlist = _db.Queryable<MatTypeinfo>();
            var mulist = _db.Queryable<MatUnitInfo>();

            var list = _db.Queryable<Material, Matattribute, MatTypeinfo, MatUnitInfo>((q, m, mt, mu) => new JoinQueryInfos(
                JoinType.Inner, q.MatPerMess == m.AttId,
                JoinType.Inner, q.MatType == mt.TypeId,
                JoinType.Inner, q.MatUnit == mu.UnitId
            ))
            .Select((q, m, mt, mu) => new MaterialDTO
            {
                MatMax = q.MatMax,
                MatMin = q.MatMin,
                MatDate = q.MatDate,
                MatType = q.MatType,
                MatDesc = q.MatDesc,
                MatName = q.MatName,
                MatState = q.MatState,
                MatBrand = q.MatBrand,
                MatModel = q.MatModel,
                UnitName = mu.UnitName,
                TypeName = mt.TypeName,
                AttName = m.AttName,
                MatPrice = q.MatPrice,
                MatPolice = q.MatPolice,
                UnitId = mu.UnitId,
                AttId = m.AttId,
                MatId = q.MatId,
                TypeId = mt.TypeId,
                MatCode = q.MatCode,
                MatImage = q.MatImage,
                MatPerMess = q.MatPerMess,
                MatUnit = mu.UnitId,
            }).WhereIF(!string.IsNullOrEmpty(matCode), q => q.MatCode.Contains(matCode))
                        .WhereIF(!string.IsNullOrEmpty(matName), q => q.MatName.Contains(matName))
                        .WhereIF(!string.IsNullOrEmpty(matModel), q => q.MatModel.Contains(matModel))
                        .WhereIF(!string.IsNullOrEmpty(matBrand), q => q.MatBrand.Contains(matBrand));

            var totalCount = list.Count();
            var pageData = list.ToPageList(pageIndex, pageSize);
            

            var result = new PageApiResut<MaterialDTO>
            {
                TotalCount = totalCount,
                Data = pageData.ToList()
            };
            return result;
        }

        /// <summary>
        /// 绑定物料属性表的下拉框
        /// </summary>
        /// <returns>返回物料属性表数据</returns>
        public List<Matattribute> GetMatattrShow()
        {
            var MatattrList = _db.Queryable<Matattribute>().ToList();
            _cSRedis.Set("MattributeShow", MatattrList);
            return MatattrList;
        }

        /// <summary>
        /// 绑定物料类别表的下拉框
        /// </summary>
        /// <returns>返回物料类别表数据</returns>
        public List<MatTypeinfo> GetMatTypeinfoShow()
        {
            var MatTypeinfoList =_db.Queryable<MatTypeinfo>().ToList();
            _cSRedis.Set("MatTypeinfoShow", MatTypeinfoList);
            return MatTypeinfoList;
        }

        /// <summary>
        /// 绑定物料单位表的下拉框
        /// </summary>
        /// <returns>返回物料单位表数据</returns>
        public List<MatUnitInfo> GetMatUnitInfoShow()
        {
            var MatUnitInfoList = _db.Queryable<MatUnitInfo>().ToList();
            _cSRedis.Set("MatUnitInfoShow", MatUnitInfoList);
            return MatUnitInfoList;
        }

        /// <summary>
        /// 添加物料表
        /// </summary>
        /// <param name="material">需要添加的物料数据</param>
        /// <returns>受影响的行数</returns>
        public int AddMaterial(Material material)
        {
            material.MatCode = YitIdHelper.NextId().ToString();
            return _db.Insertable(material).ExecuteCommand();
        }
    }
}
